package com.neusoft;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//设置允许跨域访问,"*":允许所有url跨域访问
		response.setHeader("Access-Control-Allow-Origin", "*"); 		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		
		
		boolean isLogin = false;
		
		//如果没有登录就实现登录功能
		if(!isLogin){
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");	
		
		System.out.println(userName);
		System.out.println(password);
		
		if(userName.equals("test") && password.equals("111")){
			System.out.println("登录OK");
			//创建用户信息放在cookie里
			Cookie userCookie = new Cookie("user","ok");
			//设定cookie的过期时间
			userCookie.setMaxAge(365 *24 * 60 * 60);
			//向客户端发送cookie
			response.addCookie(userCookie);
			
			
		}else{
			System.out.println("登录失败，拉黑");
		}
		}
		
		
		
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
